// pages/orderinfo/orderinfo.js
var QQMapWX = require('../../qqmap-wx-jssdk.min.js');
 
// 实例化API核心类
var qqmapsdk = new QQMapWX({
    key: '6FTBZ-OPECR-OYVWT-WULAV-V7VPQ-FAFCT' // 必填
});

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderid:"",
    conphone:"",
    // 提示交货日期
    buytime:'',
    // 地址信息
    name:'',
    phone:'',
    address:'',
    way:'',
    // 物品信息
    pic:'',
    title:'',
    value:"",
    deposit:"",
    day:"",
    sum:'',
    // 租用时间
    starttime:'',
    finishtime:'',
    // 订单信息
    orderid:'',
    ordertime:'',
    paytime:'',
    exchangetime:'',
    returntime:'',
    remark:'',
    // 快照照片
    buyarray:[],
    showpic:true,
    ismine:''
  },
  onLoad(options){

    if(options.ismine == "true"){
      this.setData({
        ismine:true
      })
    }else{
      this.setData({
        ismine:false
      })
    }

    //实现方法 @return 返回2个值，一个是带时分秒，另一个不带。
function msToDate (msec) {
  let datetime = new Date(msec);
  let year = datetime.getFullYear();
  let month = datetime.getMonth();
  let date = datetime.getDate();
  let hour = datetime.getHours();
  let minute = datetime.getMinutes();
  let second = datetime.getSeconds();

  let result1 = year + 
               '-' + 
               ((month + 1) >= 10 ? (month + 1) : '0' + (month + 1)) + 
               '-' + 
               ((date + 1) < 10 ? '0' + date : date) + 
               ' ' + 
               ((hour + 1) < 10 ? '0' + hour : hour) +
               ':' + 
               ((minute + 1) < 10 ? '0' + minute : minute) + 
               ':' + 
               ((second + 1) < 10 ? '0' + second : second);

  let result2 = year + 
               '-' + 
               ((month + 1) >= 10 ? (month + 1) : '0' + (month + 1)) + 
               '-' + 
               ((date + 1) < 10 ? '0' + date : date);

  let result = {
      hasTime: result1,
      withoutTime: result2
  };

  return result;
}
  var that = this;
  this.setData({
    orderid:options.orderid
  })

  if(options.orderid && options.itemid){
      wx.request({
        url: 'http://127.0.0.1:8080/order/orderById',
        method:"POST",
        data:{orderid:options.orderid},
        success(res){
          if(res.data){
            var data = res.data[0];
            // console.log(data)
            //记录快照照片
            var transactionPic = JSON.parse(data.transactionPic)
            //展示归还截至时间
            var newday = Date.parse(data.itemBackTime)+ 2 * 24 * 60 * 60 * 1000;
            newday= new Date(newday); 
            var backtime = msToDate(newday).withoutTime;
            
            that.setData({
              name:data.receiveName,
              phone:data.receivePhone,
              address:data.deliveryAddress,
              way:data.deliveryMethod,
              day:data.rentDays,
              sum:data.rentPayment,
              // 租用时间
              buytime:data.rentEndTime,
              starttime:data.rentStartTime,
              finishtime:data.rentEndTime,
              backdeposit:backtime,
              // 订单信息
              ordertime:data.orderGenTime,
              paytime:data.orderPayTime,
              exchangetime:data.deliveryTime,
              returntime:data.itemBackTime,
              remark:data.orderRemark,
              //交易快照
              buyarray:transactionPic,
            })
            if(!that.data.ismine){
              that.setData({
                phone:data.receivePhone,
              })
            }
            if(transactionPic != null){
              that.setData({
                showpic:false
              })
            }
          }
        }
      })
      wx.request({
        url: 'http://127.0.0.1:8080/item/selectItemById',
        method:"POST",
        data:{itemid:options.itemid},
        success(res){
          // console.log(res)
          if(res.data){
            var data = res.data[0];
            data.itemPic = JSON.parse(data.itemPic)
            if(that.data.ismine){
              that.setData({
                phone:data.contactPhone
              })
            }
            that.setData({
              // 物品信息
              pic:data.itemPic[0],
              title:data.itemTitle,
              value:data.dayRent,
              deposit:data.deposit,
              conphone:data.contactPhone
            })
          }
        }
      })
  }

  },
// // 添加商品快照
//   addpic(e){
//     let that = this;
//     wx.chooseImage({
//       count: 9,
//       success(res){
//         var picarray = that.data.buyarray.concat(res.tempFilePaths)
//         console.log(picarray)
//         that.setData({
//           buyarray:picarray,
//           showpic:false
//         })
//       }
//     })
//   },
//   //删除照片
//   delpic(e){
//     console.log(e)
//     let that = this;
//     var index = e.currentTarget.dataset.index;
//     var del = this.data.buyarray
//     wx.showModal({
//       content: '是否删除该照片',
//       title: '提示',
//       success(res){
//         if(res.confirm){
//           del.splice(index,1)
//           var len = del.length
//           if(len == 0){
//             that.setData({
//               showpic:true
//             })
//           }
//           that.setData({
//             buyarray:del
//           })
//         }
//       },
//     })
    
//   },
  // 查看图片
  showpic(e){
    var index  =e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.buyarray[index],     //当前图片地址
      urls: this.data.buyarray,                 //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 展示地址
  showaddress(e){
    var _this = this;
    //调用地址解析接口
    qqmapsdk.geocoder({
      //获取表单传入地址
      address: _this.data.address, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
      success: function(res) {//成功后的回调
        console.log(res);
        var res = res.result;
        var latitude = res.location.lat;
        var longitude = res.location.lng;
        //根据地址解析在地图上标记解析地址位置
        _this.setData({ // 获取返回结果，放到markers及poi中，并在地图展示
          markers: [{
            id: 0,
            title: res.title,
            latitude: latitude,
            longitude: longitude,
            iconPath: '/pic/地标.png',//图标路径
            width: 30,
            height: 30,
            // callout: { //可根据需求是否展示经纬度
            //   content: latitude + ',' + longitude,
            //   color: '#000',
            //   display: 'ALWAYS'
            // }
          }],
          poi: { //根据自己data数据设置相应的地图中心坐标变量名称
            latitude: latitude,
            longitude: longitude
          },
        });

        wx.openLocation({
          latitude:latitude,
          longitude:longitude,
          address: _this.data.address,
          name: _this.data.address,
          scale: 28,
        })
      },
      fail: function(error) {
        console.error(error);
      },
      complete: function(res) {
        console.log(res);
      }
    })
  },
  // 联系电话
  callrent(){
    wx.makePhoneCall({
      phoneNumber: this.data.conphone,
      success: (res) => {},
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  callregulor(){
    wx.makePhoneCall({
      phoneNumber: '1025555555',
      success: (res) => {},
      fail: (res) => {},
      complete: (res) => {},
    })
  },



})